package com.woniuxy.mesboot.controller;

import com.woniuxy.mesboot.dto.MenuDTO;
import com.woniuxy.mesboot.dto.RoleDTO;
import com.woniuxy.mesboot.service.RoleService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.TreeNodeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：lisir
 * @date ：Created in 2023/5/24 17:20
 * @description：TODO
 */
@RestController
@RequestMapping("roles")
@Slf4j
public class RoleController {

    @Resource
    private RoleService roleService;

    @PutMapping("insertRoleMenu")
    public Result insertRoleMenu(@RequestBody MenuDTO menuDTO) {
        roleService.insertRoleMenu(menuDTO);
        return Result.ok();
    }

    @GetMapping("queryMenuByRole/{roleId}")
    public Result queryMenuByRole(@PathVariable Integer roleId){
        int[] defaultChecked = roleService.selectMenuByRole(roleId);
        List<TreeNodeVO> treeNodes = roleService.selectAllMenu(0);
        return Result.ok().data("treeNodes",treeNodes).data("defaultChecked",defaultChecked);
    }

    @GetMapping("queryByUid/{uid}")
    public Result selectRoleByUserId(@PathVariable Long uid){
        int[] roleIds = roleService.selectRoleByUserId(uid);//得到当前用户拥有的角色
        List roles = roleService.selectAll();//得到所有的角色
        return Result.ok().data("checkList",roleIds).data("roleList",roles);
    }

    @PutMapping
    public Result insertRole(@RequestBody RoleDTO roleDTO){
        roleService.addRoleByUser(roleDTO);
        return Result.ok();
    }

    @GetMapping
    public Result selectAll(){
        return Result.ok().data("list",roleService.selectAll());
    }
}
